/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package obai.model.util;

import java.util.Enumeration;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import obai.control.PlataformBean;
import obai.model.detectors.MobileDeviceDetector;
import obai.model.detectors.TVDeviceDetector;

/**
 *
 * @author Fernando Arena Varella
 * @version 1.0
 */
public class UserAgentParser {

    public UserAgentParser(){

    }

    public static PlataformBean.Plataform getPlataformFromRequest(FacesContext context){
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        System.out.println("User-Agent: " + request.getHeader("user-agent"));

        try {
            if (TVDeviceDetector.isTV(request))
                return PlataformBean.Plataform.DIGITALTV;

            if (MobileDeviceDetector.isMobile(request))
                return PlataformBean.Plataform.MOBILE;

            //else returns WEB
            return PlataformBean.Plataform.WEB;

        } catch (Exception e) {
            System.out.println("Error on device detecors: " + e.getMessage());
            return PlataformBean.Plataform.WEB;
        }
//        Enumeration e;
//        for (e = request.getHeaderNames(); e.hasMoreElements();) {
//            String name = (String) e.nextElement();
//            System.out.println("Name: " + name + "   -   Value: " + request.getHeader(name));
//        }

    }

    public static void printHeaders(FacesContext context){
        System.out.println("----- Printing Headers -----");
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        Enumeration names = request.getHeaderNames();
        String name;
        while ((name = (String)names.nextElement()) != null){
            System.out.println(name + ": " + request.getHeader(name));
        }
        System.out.println("----- End of Headers -----");

        System.out.println("getPathTranslated: " + request.getPathTranslated());
        System.out.println("getPathInfo: " + request.getPathInfo());
        System.out.println("getServletPath: " + request.getServletPath());
    }

    public static void printAttributes(FacesContext context){
        System.out.println("----- Printing Attributes -----");
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        Enumeration names = request.getAttributeNames();
        String name;
        while ((name = (String)names.nextElement()) != null){
            System.out.println(name + ": " + request.getAttribute(name));
        }
    }

    public static void printParameters(FacesContext context){
        System.out.println("----- Printing Parameters -----");
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        Enumeration names = request.getParameterNames();
        String name;
        while ((name = (String)names.nextElement()) != null){
            System.out.println(name + ": " + request.getParameter(name));
        }
    }

}
